package utils

import (
    "github.com/spf13/viper"
    "time"
)
type App struct {
    TimeFormat      string  `json:"time_format"`
    JwtSecret       string  `json:"jwt_secret"`
    TokenTimeout    int64  `json:"token_timeout"`
    StaticBasePath  string  `json:"static_base_path"`
    UploadBasePath  string  `json:"upload_base_path"`
    AvatarRelPath  string `json:"avatar_rel_path"`
    ImageRelPath    string  `json:"image_rel_path"`
    ApiBaseUrl      string  `json:"api_base_url"`
}

type Server struct {
    RunMode         string  `json:"run_mode"`
    ServerAddr      string  `json:"server_addr"`
    ReadTimeout     time.Duration  `json:"read_timeout"`
    WriteTimeout    time.Duration  `json:"write_timeout"`
}

type DataBase struct {
    Mode        string  `json:"mode"`
    Host        string  `json:"host"`
    Port        string  `json:"port"`
    User        string  `json:"user"`
    Password    string  `json:"password"`
    DBName      string  `json:"db_name"`
}



var (
    ServerInfo = &Server{}
    DBInfo = &DataBase{}
    AppInfo = &App{}
)

func init()  {
    viper.AddConfigPath("conf")
    viper.SetConfigName("config")
    if err := viper.ReadInConfig(); err != nil{
        panic(err)
    }

    AppInfo.TimeFormat = viper.GetString("app.timeFormat")
    AppInfo.JwtSecret = viper.GetString("app.jwtSecret")
    AppInfo.TokenTimeout = viper.GetInt64("app.tokenTimeout")
    AppInfo.StaticBasePath = viper.GetString("app.staticBasePath")
    AppInfo.UploadBasePath = viper.GetString("app.uploadBasePath")
    AppInfo.ImageRelPath = viper.GetString("app.imageRelPath")
    AppInfo.AvatarRelPath = viper.GetString("app.avatarRelPath")
    AppInfo.ApiBaseUrl = viper.GetString("app.apiBaseUrl")

    ServerInfo.RunMode = viper.GetString("server.runMode")
    ServerInfo.ServerAddr = viper.GetString("server.serverAddr")
    ServerInfo.ReadTimeout = time.Duration(viper.GetInt("server.readTimeout")) * time.Second
    ServerInfo.WriteTimeout = time.Duration(viper.GetInt("server.writeTimeout")) * time.Second

    DBInfo.Mode = viper.GetString("database.mode")
    DBInfo.Host = viper.GetString("database.host")
    DBInfo.Port = viper.GetString("database.port")
    DBInfo.User = viper.GetString("database.user")
    DBInfo.Password = viper.GetString("database.password")
    DBInfo.DBName = viper.GetString("database.dbName")

}